<template>
  <div class="container">
    <el-form
      ref="form"
      :inline="true"
      :model="Params"
      :rules="rules"
      class="demo-form-inline"
    >
      <el-form-item label="所属学科" prop="subjectID">
        <el-select v-model="Params.subjectID" placeholder="请选择">
          <el-option
            v-for="(item, index) in simpleList"
            :key="index"
            :value="item.value"
            :label="item.label"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="目录名称" prop="tagName">
        <el-input
          v-model="Params.tagName"
          placeholder="请输入目录名称"
        ></el-input>
      </el-form-item>

      <el-form-item  >
        <el-button   @click="hClear">取消</el-button>
        <el-button   type="primary" @click="hSubmit">确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/tags'
import { simple } from '@/api/hmmm/subjects'
export default {
  props: {
    isEdit: {
      type: Boolean,
      required: true
    },
    formData: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      Params: {
        subjectID: '',
        tagName: ''
      },
      simpleList: [], // 学科列表
      rules: {
        // 校验
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ],
        tagName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }

        ]
      },
      from: {}
    }
  },
  created () {
    this.getsimpleList()
    // this.getupdate()
    // console.log(this.formData)
    if (this.isEdit === true) {
      this.Params = this.formData
    }
  },
  methods: {
    //   获取学科列表
    async getsimpleList () {
      const res = await simple()
      console.log(res)
      this.simpleList = res.data
    },
    // 添加
    async doAdd () {
      try {
        const res = add(this.Params)
        console.log('AHAH', res)
        this.$emit('add')
        this.$message.success('添加成功')
      } catch (err) {
        this.$message.error('添加失败')
      }
    },
    async doEdit () {
      try {
        await update(this.formData)
        this.$message.success('修改成功')
        this.$emit('close')
      } catch (err) {
        this.$message.error('修改失败')
        console.log(err)
      }
    },
    hSubmit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
          //   this.doAdd()
        }
      })
    },
    hClear () {
      this.$emit('close')
    }
  }
}
</script>

<style scoped >

</style>
